<?php
/*
 * @component AlphaGetCouponCode
 * @copyright Copyright (C) 2008-2009 Bernard Gilly
 * @license : GNU/GPL
 * @Website : http://www.alphaplug.com
 */

 // no direct access
defined('_JEXEC') or die('Restricted access');

$currencyleft = "";
$currencyright = "";
if ( !$this->params->get('currency_position', '0') ) 
{
	$currencyleft = $this->params->get('currency_symbol', '$');
} 
else 
{
	$currencyright = $this->params->get('currency_symbol', '&euro;');
}

if ( !$this->success ) 
{
	// start template	
	if ( $this->params->def( 'show_page_title', 1 ) ) {
	?>
	<div class="componentheading<?php echo $this->params->get( 'pageclass_sfx' ); ?>">
		<?php
		$page_title = ($this->params->get('page_title'))? $this->params->get('page_title') : $this->menuname ;
		echo $page_title;
		?>
	</div>
	<?php 
	}

	if ( $this->title || $this->description ) {
	?>
	<table class="contentpaneopen<?php echo $this->params->get( 'pageclass_sfx' ); ?>"  width="100%">
	<tr>	
		<td class="contentheading<?php echo $this->params->get( 'pageclass_sfx' ); ?>">
		<?php echo $this->title; ?>
		</td>
	</tr>
	<tr>	
		<td valign="top">
		<?php echo JHTML::_('content.prepare', $this->description ); ?>
		</td>
	</tr>
	</table>
	<?php
	}	
	?>
	<table class="contentpaneopen<?php echo $this->params->get( 'pageclass_sfx' ); ?>" width="100%">
	<tr>
		<td colspan="6"><?php echo JText::_( 'AGCC_AVAILABLE_COUPONS' ); ?></td>		
	</tr>
	<tr>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>" width="15px"><div align="center"><?php echo JText::_( 'AGCC_NUM' ); ?></div></td>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>"><?php echo JText::_( 'AGCC_VALUE_OF_THE_COUPON' ); ?></td>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>"><?php echo JText::_( 'AGCC_NUMBER_OF_POINTS' ); ?></td>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>"><?php echo JText::_( 'AGCC_VALIDITY' ); ?></td>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>">&nbsp;</td>
		<td class="sectiontableheader<?php echo $this->params->get( 'pageclass_sfx' ); ?>"><?php echo sprintf( JText::_( 'AGCC_YOU_CURRENTLY_HAVE_X_POINTS' ), intval($this->currenttotalpoints) ); ?></td>
	</tr>	
	<?php
	$unitpoints = $this->params->get('unit_points');
	$discount = $this->params->get('discount_amount', 0);
	$percent = $this->params->get('discount_percent', 0);
	$url = "index.php?option=com_alphagetcouponcode&amp;task=purchase&amp;id=";
	$k = 0;
	$javascript = "onclick=\"return confirm('" . JText::_( 'AGCC_DO_YOU_WANT_TO_REDEEM' ) . "')\"";
	
	for ($i=0, $n=count( $this->couponscode ); $i < $n; $i++)	
	{
		$couponcode = $this->couponscode[$i];
		$numpoints = $couponcode->coupon_value*intval($unitpoints);	
		if ( $discount && ($couponcode->coupon_value >= $discount ) && $percent )
		{
			// recalculate number of points necessary			
			$op = intval(($numpoints*$percent)/100);
			$numpoints = $numpoints-$op;
		}		
		$urlCoupon = $url . $couponcode->coupon_id . "&amp;points=" . $numpoints . "&amp;type=" . $couponcode->coupon_type . "&amp;Itemid=" . $this->menuid ;
		
		$getThisCouponCode = "<a href=\"". JRoute::_( $urlCoupon ) . "\" $javascript>" . JText::_( 'AGCC_GET_THIS_COUPON' ) . "</a>";
		
		$fontstart = "";
		$fontend   = "";
		
		$showbutton = 1;
		
		if ( $this->currenttotalpoints < $numpoints ) 
		{
			$getThisCouponCode = "<i>" . JText::_( 'AGCC_YOU_DO_NOT_HAVE_ENOUGH_POINTS') . "</i>";
			$fontstart = "<font color=\"grey\">";
			$fontend   = "</font>";
			$showbutton = 0;
		} 
		
		if ( checkCoupon( $this->referreid, $couponcode->coupon_code ) ) 
		{
			$getThisCouponCode = "<font color=\"red\"><i>" . JText::_( 'AGCC_ALREADY_PURCHASED') . "</i></font>";
			$fontstart = "<font color=\"grey\">";
			$fontend   = "</font>";
			$showbutton = 0;
		}
		
		$validity = ( $couponcode->coupon_type=='gift' )? JText::_( 'AGCC_USED_ONLY_ONCE' ) : JText::_( 'AGCC_PERMANENT' ) ;
		?>
		<tr>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">
				<div align="center"><?php echo $fontstart . intval($i+1) . $fontend ; ?></div>
			</td>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">
				<?php echo $fontstart . $currencyleft . " " . $couponcode->coupon_value . " " . $currencyright . $fontend ; ?>
			</td>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">
			<?php echo $fontstart . $numpoints . ' ' . JText::_( 'AGCC_POINTS' ) . $fontend ; ?>
			</td>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">
			<?php echo $fontstart . $validity . $fontend ; ?>
			</td>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">&nbsp;
			
			</td>
			<td class="sectiontableentry<?php echo $k . $this->params->get( 'pageclass_sfx' ); ?>">
			<?php 
			if ( !$showbutton ) {
				echo $fontstart . $getThisCouponCode . $fontend ; 
			} else {
			?>
			<form action="<?php echo JRoute::_( 'index.php' );?>" method="post" name="purchaseCouponVM<?php echo $i ;?>" id="purchaseCouponVM<?php echo $i ;?>">
				<input type="submit" name="Submit" value="<?php echo JText::_( 'AGCC_GET_THIS_COUPON' ) ;?>" class="button" <?php echo $javascript ?>/>
			  	<input type="hidden" name="option" value="com_alphagetcouponcode" />
				<input type="hidden" name="task" value="purchase" />
				<input type="hidden" name="Itemid" value="<?php echo $this->menuid ;?>" />		
				<input type="hidden" name="type" value="<?php echo $couponcode->coupon_type ;?>" />		
				<input type="hidden" name="points" value="<?php echo $numpoints ;?>" />		
				<input type="hidden" name="id" value="<?php echo $couponcode->coupon_id ;?>" />				
			</form>
			<?php } ?>
			</td>
		</tr>
		<?php
		//$i++;
		$k = 1 - $k;
	} 
	?>
	</table>
<?php
} 
 else 
{ 
	// It's ok, now show the code for the user
	if ( $this->params->def( 'show_page_title', 1 ) ) {
	?>
	<div class="componentheading<?php echo $this->params->get( 'pageclass_sfx' ); ?>">
		<?php
		$page_title = ($this->params->get('page_title'))? $this->params->get('page_title') : $this->menuname ;
		echo $page_title;
		?>
	</div>
	<?php 
	}
?>
	<table class="contentpaneopen<?php echo $this->params->get( 'pageclass_sfx' ); ?>">
	<tr>	
		<td valign="top">
		<?php echo JText::_( 'AGCC_YOUR_NEW_TOTAL_POINTS' ) . " <b>" . $this->currenttotalpoints . "</b>" ; ?><br />
		<?php echo JText::_( 'AGCC_YOUR_COUPON_CODE' ) . " <b>" . $this->codecoupon ."</b>" ; ?><br />
		<?php echo JText::_( 'AGCC_YOU_CAN_FIND_YOUR_COUPON_CODE_IN_ACCOUNT' ); ?>
		</td>
	</tr>
	</table>
<?php
}

	/** 
	*
	*  Provide copyright on frontend
	*  If you remove or hide this line below,
	*  please make a donation if you find AlphaBuyPoints usefull
	*  and want to support its continued development.
	*  Your donations help by hardware, hosting services and other expenses that come up as we develop,
	*  protect and promote AlphaUserPoints and other free components.
	*  You can donate on http://www.alphaplug.com
	*
	*/	
	getCopyrightNotice ();


 
function checkCoupon( $referreid, $coupon_code )
{
	$db	=& JFactory::getDBO();
	
	$query = "SELECT id FROM #__alpha_userpoints_details WHERE `referreid`='$referreid' AND `keyreference`='$coupon_code' LIMIT 1";
	$db->setQuery( $query );
	$result = $db->loadResult();
	
	if ( $result ) 
	{ 
		return true;
	} else return false;

}
?>